package com.yiqi.zhiyuan.feature.msg.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.DensityUtil;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.vo.MessageTypeVO;
import com.yiqi.zhiyuan.entity.vo.MsgVO;
import com.yiqi.zhiyuan.feature.msg.contract.MessageContract;
import com.yiqi.zhiyuan.feature.msg.dependencies.message.DaggerMessageComponent;
import com.yiqi.zhiyuan.feature.msg.dependencies.message.MessagePresenterModule;
import com.yiqi.zhiyuan.feature.msg.view.adapter.EarningsMessageAdapter;
import com.yiqi.zhiyuan.feature.msg.view.adapter.MessageAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageActivity extends BaseActivity implements MessageContract.View, OnRefreshListener, View.OnClickListener, BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_msg)
    RecyclerView mRvMsg;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    private EmptyView mEmptyView;
    private MessageAdapter mAdapter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_message;
    }

    @Inject
    MessageContract.Presenter mPresenter;

    @Override
    protected void initInjector() {
        DaggerMessageComponent.builder().messagePresenterModule(new MessagePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "消息通知", true, true);
        initAdapter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.doNotification();
    }

    @Override
    public void showMessageSuccess(MsgVO list) {
        mSmartRefreshLayout.finishRefresh();
        mAdapter.setNewData(list.getList());
        mAdapter.setMsgVO(list);

    }

    @Override
    public void showMessageListSuccess(List<MessageTypeVO> list) {

    }

    @Override
    public void showMessageListError(String errorMsg) {
        mSmartRefreshLayout.finishRefresh();
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(mEmptyView.getErrorView(errorMsg));
    }

    @Override
    public void showMessageListEmpty() {
        mSmartRefreshLayout.finishRefresh();
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
    }

    @Override
    public void showMessageListMoreSuccess(List<MessageTypeVO> list) {

    }

    @Override
    public void showMessageListMoreError(String errorMsg) {

    }

    private void initAdapter() {
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvMsg);
        mEmptyView.setErrorButtonClickListener(this);
        mRvMsg.setLayoutManager(new LinearLayoutManager(this));
        mRvMsg.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(DensityUtil.dp2px(1)));
        mAdapter = new MessageAdapter();
        mAdapter.bindToRecyclerView(mRvMsg);
        mAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        mPresenter.doNotification();
    }

    @Override
    public void onClick(View v) {
          mPresenter.doNotification();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, position == 3 ? MsgEarningListActivity.class : MsgListActivity.class);
        intent.putExtra("message_type", mAdapter.getItem(position).getType());
        startActivity(intent);

    }
}
